<?php

/**
 * YUtil 帮助类
 *
 * @author Hua Yang <htmlcook@gmail.com>
 * @package common.helpers
 * @since 1.0
 */
class YUtil
{
	const MINUTE_IN_SECONDS = 60;
	const HOUR_IN_SECONDS = 3600;
	const DAY_IN_SECONDS = 86400;
	const WEEK_IN_SECONDS = 604800;
	const YEAR_IN_SECONDS = 31536000;

	public static function humanTimeDiff($from, $to = null)
	{
		if ($to === null) {
			$to = time();
		}

		$diff = (int)abs($to - $from);
		switch (true) {
			case $diff < self::HOUR_IN_SECONDS:
				$mins = round($diff / self::MINUTE_IN_SECONDS);
				if ($mins < 1) {
					$mins = 1;
				}
				$since = sprintf('%s分钟前', $mins);
				break;
			case $diff < self::DAY_IN_SECONDS:
				$hours = round($diff / self::HOUR_IN_SECONDS);
				if ($hours < 1) {
					$hours = 1;
				}
				$since = sprintf('%s小时前', $hours);
				break;
			case $diff < self::WEEK_IN_SECONDS;
				$days = round($diff / self::DAY_IN_SECONDS);
				if ($days < 1) {
					$days = 1;
				}
				$since = sprintf('%s天前', $days);
				break;
			case $diff < 30 * self::DAY_IN_SECONDS;
				$weeks = round($diff / self::WEEK_IN_SECONDS);
				if ($weeks < 1) {
					$weeks = 1;
				}
				$since = sprintf('%s周前', $weeks);
				break;
			case $diff < self::YEAR_IN_SECONDS;
				$months = round($diff / (30 * self::DAY_IN_SECONDS));
				if ($months < 1) {
					$months = 1;
				}
				$since = sprintf('%s个月前', $months);
				break;
			case $diff > self::YEAR_IN_SECONDS;
				$years = round($diff / self::YEAR_IN_SECONDS);
				if ($years < 1) {
					$years = 1;
				}
				$since = sprintf('%s年前', $years);
				break;
		}
		return $since;
	}

	/**
	 * 截取字符串长度
	 *
	 * @param string $str
	 * @param integer $length
	 * @param string $more
	 * @return string
	 */
	public static function substr($str, $length, $more = '...')
	{
		$oLength = mb_strlen($str, Yii::app()->charset);
		if ($oLength <= $length) {
			return $str;
		} else {
			return mb_substr($str, 0, $length, Yii::app()->charset) . $more;
		}
	}

	/**
	 * 时间过滤列表
	 *
	 * @return array
	 */
	public static function timeFilterList()
	{
		return array(
			'-3 day' => '最近三天',
			'-1 week' => '最近一个星期',
			'-1 month' => '最近一个月',
			'-3 month' => '最近三个月',
			'-1 year' => '最近一年',
		);
	}

	/**
	 * 生成时间条件
	 *
	 * @param string $value 时间字符串
	 * @param string $attribute 模型属性
	 * @return string
	 */
	public static function generateTimeCondition($value, $attribute)
	{
		$time = strtotime($value);
		if ($time == false) {
			return;
		}
		$now = time();
		return "{$attribute} BETWEEN $time AND $now";
	}

	/**
	 * 生成文件夹
	 *
	 * @param string $dst 目标
	 * @param array $options 选项
	 * @param boolean $recursive 是否递归
	 * @return boolean
	 */
	public static function mkdir($dst, array $options, $recursive)
	{
		$prevDir = dirname($dst);
		if ($recursive && !is_dir($dst) && !is_dir($prevDir)) {
			self::mkdir(dirname($dst), $options, true);
		}

		$mode = isset($options['newDirMode']) ? $options['newDirMode'] : 0777;
		$res = mkdir($dst, $mode);
		@chmod($dst, $mode);
		return $res;
	}

	public static function lcfirst($upper) {
		if (function_exists('lcfirst')) {
			return lcfirst($upper);
		} else {
			return strtolower(substr($upper,0, 1)) . substr($upper, 1);
		}
	}
}